export default {
  namespace: 'cinemaList',

  state: {
    list: []
  },

  // 异步处理数据
  effects: {
    *getList(action: any, obj: any): any {
      // 结构出 saga 中的 call 用于异步请求，put 用于返回数据
      const {call, put} = obj
      let res = yield call(getCinemaList, action.payload.cityId)

      yield put({
        type:'changeList',
        payload: res
      })
    }
  },

  // 异步处理完数据后，同步返回数据
  reducers:{
    changeList(prevState:any, action:any){
      return {
        ...prevState,
        list: action.payload
      }
    },

    // 清除数据
    clearList(prevState:any){
      return{
        ...prevState,
        list:[]
      }

    }
  }
}

async function getCinemaList(cityId:any) {
  let res = await fetch(`https://m.maizuo.com/gateway?cityId=${cityId}&ticketFlag=1&k=7667872`, {
    headers: {
      'X-Client-Info': '{"a":"3000","ch":"1002","v":"5.2.0","e":"1640782700594797135921153"}',
      'X-Host': 'mall.film-ticket.cinema.list'
    }
  }).then(res => res.json())
    // .then(res => {
    //   console.log(res.data.cinemas)
    // })

  return res.data.cinemas
}
